package com.woniu.ucenterservice.service.imp;

import com.woniu.ucenterservice.dao.FavoriteMapper;
import com.woniu.ucenterservice.dao.GoodsMapper;
import com.woniu.ucenterservice.dao.HistoryMapper;
import com.woniu.ucenterservice.pojo.Favorite;
import com.woniu.ucenterservice.pojo.Goods;
import com.woniu.ucenterservice.pojo.History;
import com.woniu.ucenterservice.service.MyGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyGoodsImp implements MyGoods {
    @Autowired
    HistoryMapper historyMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    FavoriteMapper favoriteMapper;
    @Override
    @Transactional
    public List<Goods> selectMyGoods(Integer userId) {
        List<History> histories = historyMapper.selectByUid(userId);
      List goods=new ArrayList();
      for (History history:histories){
          Goods good = goodsMapper.selectByPrimaryKey(history.getGoodsId());
          goods.add(good);
      }
        return goods;
    }

    @Override
    public List<Goods> selectFavGoods(Integer userId) {
        List<Favorite> favorites = favoriteMapper.selectFavByUid(userId);
        List goods=new ArrayList();
        for (Favorite favorite:favorites){
            Goods good = goodsMapper.selectByPrimaryKey(favorite.getGoodsId());
            goods.add(good);
        }
        return goods;
    }

    @Override
    public void deleteMyfav(Integer goodsId) {
        favoriteMapper.deleteByGoodId(goodsId);
    }

    @Override
    public void batchDeleteMyFav(List<Integer> list) {
        favoriteMapper.batchDeleteMyFav(list);
    }

    @Override
    public void deleteMyGoods(Integer goodsId) {
        historyMapper.deleteByMyGoodId(goodsId);
    }

    @Override
    public void batchDeleteMyGoods(List<Integer> list) {
        historyMapper.batchDeleteMyGoods(list);
    }

    @Override
    public List<History> selectAll(Integer userId) {
        List<History> histories = historyMapper.selectByUid(userId);

        return histories;
    }
}
